package com.tiantian.quanminxiu.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.adapter.FanRollAdapter;
import com.tiantian.quanminxiu.adapter.FanRollGuardAdapter;
import com.tiantian.quanminxiu.module.Module_LinearLayoutManager;
import com.tiantian.quanminxiu.presenter.FanRollPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.FanRollView;

import org.json.JSONArray;
import org.json.JSONObject;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/12.
 */
public class Activity_FanRoll extends ActivityBase implements FanRollView{

    public final static String LIVE = "Live";   //场榜
    public final static String WEEK = "Week";   //周榜
    public final static String TOTAL = "";      //总榜

    private FanRollPresent present;

    private RecyclerView rvGuard,rvFanRoll;
    private FanRollAdapter fanRollAdapter;
    private FanRollGuardAdapter fanRollGuardAdapter;
    private JSONArray fanrollList,GuardList;
    private View rlFanScene,rlFanWeek,rlFanTotal,vFanScene,vFanWeek,vFanTotal;
    private TextView txtFanScene,txtFanWeek,txtFanTotal,txtAnchorGuard;

    private int roomId;
    private String NickName;
    private String Type = LIVE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fanroll);

        Init();
        InitView();

        roomId =getIntent().getIntExtra("roomId",-1);
        NickName =getIntent().getStringExtra("NickName");
        if(roomId<0){
            CommonUtils.showToask(thisContext,"房间号出错");
            finish();
        }

        present = new FanRollPresent();
        present.attachView(this);

        UserData.Reload.Activity_FanRoll = true;
        mSwipeRefresh.setOnRefreshListener(() -> {
            UserData.Reload.Activity_FanRoll = true;
            PageInit();
        });
    }

    @Override
    public void InitView() {
        rvGuard = (RecyclerView) findViewById(R.id.rvGuard);
        rvFanRoll = (RecyclerView) findViewById(R.id.rvFanRoll);

        rlFanScene = findViewById(R.id.rlFanScene);
        rlFanWeek = findViewById(R.id.rlFanWeek);
        rlFanTotal = findViewById(R.id.rlFanTotal);
        vFanScene = findViewById(R.id.vFanScene);
        vFanWeek = findViewById(R.id.vFanWeek);
        vFanTotal = findViewById(R.id.vFanTotal);

        txtFanScene = (TextView) findViewById(R.id.txtFanScene);
        txtFanWeek = (TextView) findViewById(R.id.txtFanWeek);
        txtFanTotal = (TextView) findViewById(R.id.txtFanTotal);
        txtAnchorGuard = (TextView) findViewById(R.id.txtAnchorGuard);

        Module_LinearLayoutManager UserAvatarLinearLayout = new Module_LinearLayoutManager(thisContext);
        UserAvatarLinearLayout.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvGuard.setLayoutManager(UserAvatarLinearLayout);
        rvFanRoll.setLayoutManager(new Module_LinearLayoutManager(thisContext));

        RxView.clicks(rlFanScene).map(View->rlFanScene).subscribe(OC_Botton);
        RxView.clicks(rlFanWeek).map(View->rlFanWeek).subscribe(OC_Botton);
        RxView.clicks(rlFanTotal).map(View->rlFanTotal).subscribe(OC_Botton);
    }

    //region PageInit
    @Override
    public void PageInit() {
        if(!UserData.Reload.Activity_FanRoll){
            return;
        }
        UserData.Reload.Activity_FanRoll = false;
        present.getFanRollList(roomId+"",Type);
        present.getGuardList(roomId + "");
    }
    //endregion

    //region OC_Botton
    private Action1<View> OC_Botton = v->{
        switch (v.getId()){
            case R.id.rlFanScene:
                vFanScene.setVisibility(View.VISIBLE);
                vFanWeek.setVisibility(View.GONE);
                vFanTotal.setVisibility(View.GONE);
                txtFanScene.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.orange));
                txtFanWeek.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.grey3));
                txtFanTotal.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                Type = LIVE;
                present.getFanRollList(roomId+"",Type);
                break;
            case R.id.rlFanWeek:
                vFanWeek.setVisibility(View.VISIBLE);
                vFanScene.setVisibility(View.GONE);
                vFanTotal.setVisibility(View.GONE);
                txtFanWeek.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.orange));
                txtFanScene.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.grey3));
                txtFanTotal.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                Type = WEEK;
                present.getFanRollList(roomId+"",Type);
                break;
            case R.id.rlFanTotal:
                vFanTotal.setVisibility(View.VISIBLE);
                vFanWeek.setVisibility(View.GONE);
                vFanScene.setVisibility(View.GONE);
                txtFanTotal.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.orange));
                txtFanWeek.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.grey3));
                txtFanScene.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                Type = TOTAL;
                present.getFanRollList(roomId+"",Type);
                break;
        }
    };
    //endregion

    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    public void getFanRollList(JSONArray fanArray) {
        fanrollList = new JSONArray();
        for (int i=0;i<fanArray.length();i++){
            JSONObject object = fanArray.optJSONObject(i);
            fanrollList.put(object);
        }
        fanRollAdapter = new FanRollAdapter(thisActivity,fanrollList,Type);
        rvFanRoll.setAdapter(fanRollAdapter);
    }

    @Override
    public void getGuradList(JSONArray guardArray) {
        GuardList = new JSONArray();
        txtAnchorGuard.setText("他的守护("+(guardArray.length())+")");
        //第一个数据用于开通守护
        GuardList.put(new JSONObject());
        for (int i=0;i<guardArray.length();i++){
            JSONObject object = guardArray.optJSONObject(i);
            GuardList.put(object);
        }
        fanRollGuardAdapter = new FanRollGuardAdapter(thisActivity,GuardList,roomId,NickName);
        rvGuard.setAdapter(fanRollGuardAdapter);
    }
}
